#include "Journal.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QTextEdit>
#include <QLineEdit>
#include <QPushButton>
#include "Product.h"
#include <QMessageBox>
#include<QDate>

/* class CalcWindow definition */

Journal::Journal(QWidget *parent, Bridget *bd) :
        QWidget(parent),
        addButton(tr("Add"), this)
{
    choosePar=new QComboBox(this);
    choosePar->addItem("Eating");
    choosePar->addItem("Activity");


    connect(choosePar, SIGNAL(activated(int)), this, SLOT(setPar(int)));


    act1=new Act(bd);
    allFood = new Product;
    allFood->setName(date.currentDate().toString());
    allFood->setCarbohydrate(0);
    allFood->setEnergy(0);
    allFood->setFat(0);
    allFood->setProtein(0);
    QFile journal("journal.xml");
    QFile file("baseProduct.xml");
    if (file.open(QIODevice::ReadOnly))
    {
        if (docJournal.setContent(&file))
        {
            QDomElement root = docJournal.documentElement();
            QDomElement child = root.firstChildElement("product");
            while (!child.isNull())
            {
                inputProduct.addItem(child.attribute("name", ""));
                child = child.nextSiblingElement("product");
            }

        }

     file.close();
     }

    act=new Act(bd);
    act->setName(date.currentDate().toString());
    act->setTime(0);

    QFile fileA("baseAct.xml");
    if (fileA.open(QIODevice::ReadOnly))
    {
        if (docJournal.setContent(&fileA))
        {
            QDomElement root = docJournal.documentElement();
            QDomElement child = root.firstChildElement("act");
            while (!child.isNull())
            {
                inputAct.addItem(child.attribute("name", ""));
                child = child.nextSiblingElement("act");
            }

        }

     fileA.close();
     }



    //inputProduct.setEditable("true");
    inputProduct.setEditable(true);
    inputProduct.clearEditText();

    inputAct.setEditable("true");
    inputAct.clearEditText();






    QVBoxLayout *mainLayout = new QVBoxLayout(this);

    inputProductLabel.setText("Product:");
    inputWeightLabel.setText("Weight, kg:");

    inputActLabel.setText("Action:");
    inputTimeLabel.setText("Time, min:");

    QHBoxLayout *l1 = new QHBoxLayout();

    l1->addWidget(&inputProductLabel);
    l1->addWidget(&inputProduct,inputAct.width());

    QHBoxLayout *l2 = new QHBoxLayout();
    l2->addWidget(&inputWeightLabel);
    l2->addWidget(&inputWeight,inputAct.width());

    QHBoxLayout *l3 = new QHBoxLayout();
    l3->addWidget(&inputActLabel);
    l3->addWidget(&inputAct,inputAct.width());

    QHBoxLayout *l4 = new QHBoxLayout();
    l4->addWidget(&inputTimeLabel);
    l4->addWidget(&inputTime,inputAct.width());


    mainLayout->addWidget(choosePar);
    mainLayout->addLayout(l1);
    mainLayout->addLayout(l2);
    mainLayout->addLayout(l3);
    mainLayout->addLayout(l4);

    mainLayout->addWidget(&addButton);
    mainLayout->addStretch();


    connect(&addButton, SIGNAL(clicked()),
            this, SLOT(onAdd()));

setPar(0);
}

void Journal::onAdd()
{

  /*  if (inputProduct.currentText() == 0)
    {
        QMessageBox::information(this, tr("Bridget"), tr("No input product."));
        return;
    }
    if (inputWeight.text().size() == 0)
    {
        QMessageBox::information(this, tr("Bridget"), tr("No input weight."));
        return;
    }*/
if ((inputProduct.currentText() != 0)&& (inputWeight.text().size() != 0) &&
        (inputWeight.text().toInt()>0)&&(inputWeight.text().toInt()<10000))
    {

    product=new Product;
    product->setName(inputProduct.currentText());
    product->setWeight(inputWeight.text().toInt());
    if (product->setPar())
    {

        doRecord();
        QMessageBox::information(this, tr("Bridget"), tr("The product record added in journal."));

    }
    else
    {
        QMessageBox::information(this, tr("Bridget"), tr("This product is absent in base."));
        return;
    }

//setEating();
}


if ((inputAct.currentText() != 0)&& (inputTime.text().size() != 0)
    &&
            (inputTime.text().toInt()>0)&&(inputTime.text().toInt()<1440))

    {

    act1->setName(inputAct.currentText());
    act1->setTime(inputTime.text().toDouble());
    qDebug()<<"Weight in act="<<act->getWeight();
    if (act1->setPar())
    {

        doRecordAct();
        QMessageBox::information(this, tr("Bridget"), tr("The action record added in journal."));

    }
    else
    {
        QMessageBox::information(this, tr("Bridget"), tr("This action is absent in base."));
        return;
    }

//setEating();
}

}

void Journal::doRecord()
{
    QDate date;
    QTime curTime;


    QFile file("Journal.xml");
    QFile file1 ("tempJournal.xml");
    if (file.open(QIODevice::ReadWrite))
    {
        if (docJournal.setContent(&file))
        {


            const int Indent = 4;
            QDomElement root = docJournal.documentElement();
            QDomElement food = docJournal.createElement("food");
            food.setAttribute("date",date.currentDate().toString());

            food.setAttribute("time",curTime.currentTime().toString());

           // QDomElement time = docJournal.createElement("time");

            QDomElement protein = docJournal.createElement("protein");
            QDomElement fat = docJournal.createElement("fat");
            QDomElement carbohydrate = docJournal.createElement("carbohydrate");
            QDomElement energy = docJournal.createElement("energy");

          //  QDomText timeText = docJournal.createTextNode(curTime.currentTime().toString());

            QDomText fatText = docJournal.createTextNode(QString::number(product->getFat()));
            QDomText carbohydrateText = docJournal.createTextNode(QString::number(product->getCarbohydrate()));
            QDomText proteinText = docJournal.createTextNode(QString::number(product->getProtein()));
            QDomText energyText = docJournal.createTextNode(QString::number(product->getEnergy()));

            root.appendChild(food);

           /* food.appendChild(time);
            time.appendChild(timeText);*/

            food.appendChild(protein);
            protein.appendChild(proteinText);
            food.appendChild(fat);
            fat.appendChild(fatText);
            food.appendChild(carbohydrate);
            carbohydrate.appendChild(carbohydrateText);
            food.appendChild(energy);
            energy.appendChild(energyText);
            if (file1.open(QIODevice::ReadWrite))
            {
            QTextStream out(&file1);
            docJournal.save(out, Indent);
            qDebug()<<"File1";
        }
        }
        file.close();
        file1.close();
        file.remove();
        file1.rename("Journal.xml");

    }
}


void Journal::doRecordAct()
{
    QDate date;

    QFile file("Journal.xml");
    QFile file1 ("tempJournal.xml");
    if (file.open(QIODevice::ReadWrite))
    {
        if (docJournal.setContent(&file))
        {


            const int Indent = 4;
            QDomElement root = docJournal.documentElement();
            QDomElement action = docJournal.createElement("act");
            action.setAttribute("date",date.currentDate().toString());
            QDomElement energy = docJournal.createElement("energy");
            QDomText energyText = docJournal.createTextNode(QString::number(act1->getEnergy()));

            root.appendChild(action);
            action.appendChild(energy);
            energy.appendChild(energyText);

            if (file1.open(QIODevice::ReadWrite))
            {
            QTextStream out(&file1);
            docJournal.save(out, Indent);
            qDebug()<<"File1";
        }
        }
        file.close();
        file1.close();
        file.remove();
        file1.rename("Journal.xml");

    }
}



void Journal::setEating()
{

    double Protein, Fat, Carbohydrate, Energy;
    QDate date;
    QFile file("Journal.xml");
    if (file.open(QIODevice::ReadOnly))
    {
        if (docJournal.setContent(&file))
        {
            QDomElement root = docJournal.documentElement();
            QDomElement child = root.firstChildElement("food");

            while (!child.isNull())
            {
                if (child.attribute("date", "")==date.currentDate().toString())
                {
                    Protein = child.firstChildElement("protein").text().toDouble();
                    Fat=child.firstChildElement("fat").text().toDouble();
                    Carbohydrate = child.firstChildElement("carbohydrate").text().toDouble();
                    Energy = child.firstChildElement("energy").text().toDouble();
                    allFood->setProtein((allFood->getProtein()+Protein));
                    allFood->setFat((allFood->getFat()+Fat));
                    allFood->setCarbohydrate((allFood->getCarbohydrate()+Carbohydrate));
                    allFood->setEnergy((allFood->getEnergy()+Energy));
                }
                child = child.nextSiblingElement("food");


            }

}
           else
            {

               qDebug()<<"No setContent!";
            }
            file.close();
            }

}

Product *Journal::getAllFood()
{
    return allFood;
}

void Journal::setPar(int t)
{
    switch (t)
           {
                   case 0:  inputProduct.setVisible(true);
                            inputProductLabel.setVisible(true);
                            inputWeight.setVisible(true);
                            inputWeightLabel.setVisible(true);
                            inputAct.clearEditText();
                            inputAct.setVisible(false);
                            inputActLabel.setVisible(false);
                            inputTime.clear();
                            inputTime.setVisible(false);
                            inputTimeLabel.setVisible(false);
                                    break;

                   case 1:  inputProduct.clearEditText();
                            inputProduct.setVisible(false);
                            inputProductLabel.setVisible(false);
                            inputWeight.clear();
                            inputWeight.setVisible(false);
                            inputWeightLabel.setVisible(false);
                            inputAct.setVisible(true);
                            inputActLabel.setVisible(true);
                            inputTime.setVisible(true);
                            inputTimeLabel.setVisible(true);
                                    break;


                   default: break;
           }
}

